package design_pattern.study.config_factory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CofferFactory {
    //加载配置文件，获取配置文件中配置的全类名，并创建该类的对象进行存储
    //1,定义容器对象存储咖啡对象
    private static final Map<String, Coffer> map = new HashMap<>();


    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src/bean.properties"));
            Set<Object> keys = properties.keySet();
            for (Object key:keys){
                String className = properties.getProperty((String) key);
                Class<?> aClass = Class.forName(className);
                Coffer coffer = (Coffer)aClass.newInstance();
                map.put((String) key,coffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据名称获取对象
    public static Coffer getCoffer(String name){
        return map.get(name);
    }
}
